﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;

public partial class WebCustomUserControls_AutoGeneIdentifyNo : System.Web.UI.Page
{
    private int iQty = 0;
    private int iFlag = 0;//查询临时的识别码表
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            //this.txtTest.Text = Page.Request["ParBod_No"].ToString() + "*" + Page.Request["ParBod_Date"].ToString() + "*" + Page.Request["ParIm_No"].ToString() + "*" + Page.Request["ParBatch_no"].ToString();
            //this.TextBox1.Text = Page.Request["ParBod_No"].ToString() + "*" + Page.Request["ParBod_Date"].ToString(); 
            //this.TextBox6.Text = Page.Request["ParIm_No"].ToString() + "*" + Page.Request["ParBatch_no"].ToString();
            ClsAutoGeneNoDAL cls_AutoGeneNoDAL = new ClsAutoGeneNoDAL();
            Boolean bSuccess = cls_AutoGeneNoDAL.DeleteTempIdentifys();

            this.labhidPara.Text = Page.Request["ParBod_No"].ToString() + "*" + Page.Request["ParBod_Date"].ToString() + "*"
                + Page.Request["ParIm_No"].ToString() + "*" + Page.Request["ParBatch_no"].ToString();

            string [] arrPara = getParams();
            //打开时查询是否存在于公共识别码表中
            //ClsAutoGeneNoDAL cls_AutoGeneNoDAL = new ClsAutoGeneNoDAL();
            DataSet dst = cls_AutoGeneNoDAL.CheckIsGeneraTionIdentifys(arrPara[0], DateTime.Parse(arrPara[1]), arrPara[2], arrPara[3]);
            if (dst != null && dst.Tables[0].Rows.Count > 0)
            {
                this.dgrdIdentifyResult.DataSource = dst;
                this.dgrdIdentifyResult.DataBind();
            }
            else
            { 
                this.lbtnQueryFirst.Visible = this.lbtnQueryPrev.Visible = this.lbtnQueryNext.Visible = this.lbtnQueryEnd.Visible = false;
                this.lbtnQueryFirst.Enabled = this.lbtnQueryPrev.Enabled = this.lbtnQueryNext.Enabled = this.lbtnQueryEnd.Enabled = false;
            }

            //如果公共识别码表中已经存在识别码，则不可以进行相关操作。
            if (dst.Tables[0].Rows.Count >0)
            {
               this.labCurQty.Text = "";
               this.btnContinue.Enabled = this.btnContinue.Enabled = this.btnUpdate.Enabled = this.btnGeneration.Enabled = false;
               this.btnClear.Enabled = this.btnClearCur.Enabled = this.btnDel.Enabled = this.btnOk.Enabled = false;
               //保存到SESSION当中
               ClsPropertyDAL cls_Property = new ClsPropertyDAL();
               cls_Property.SetOrGetQueryDataSet = dst;               
            }
            this.btnGeneration.Enabled = this.btnUpdate.Enabled = false;                       
        }
        this.labMessage.Text = "";            
        iQty = int.Parse(Page.Request["ParQty"].ToString());
        this.labQty.Value = iQty.ToString();//商品数量
        this.labCurQty.Text = iQty.ToString();
       
    }

    //得到临时识别码表中的记录
    private void getIdentifyNo(int iFlag)
    {
        ClsAutoGeneNoDAL cls_AutoGeneNoDAL = new ClsAutoGeneNoDAL();
        DataSet dst = cls_AutoGeneNoDAL.GetIdentifyNo(iFlag);
        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            this.dgrdIdentifyResult.DataSource = dst;
            this.dgrdIdentifyResult.DataBind();
            this.labAlreadyQty.Text = dst.Tables[0].Rows.Count.ToString();            
        }

        //保存到SESSION当中
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        cls_Property.SetOrGetQueryDataSet = dst;
    }

    //批量录入
    protected void btnGeneration_Click(object sender, EventArgs e)
    {
        ClsAutoGeneNoDAL cls_AutoGeneNoDAL = new ClsAutoGeneNoDAL();        

        string strPrefix = this.txtPrefix.Text.Trim();
        string strSuffix = this.txtSuffix.Text.Trim();
        int iBeginNo = int.Parse(this.txtBeginNo.Text.Trim());
        int iEndNo = int.Parse(this.txtEndNo.Text.Trim());
        //隐藏值：单号，日期，商品编号，批号     
        string[] arrPara = getParams();
        Boolean bSuccess = cls_AutoGeneNoDAL.AutoGenarationIdentify(strPrefix, strSuffix, iBeginNo, iEndNo, arrPara[0], DateTime.Parse(arrPara[1]), arrPara[2], arrPara[3]);
        if (bSuccess)
        {
            getIdentifyNo(iFlag);//此处代表查询批量产生的识别码
            ChangPage();
        }
        
    }

    //保存到公共识别码表
    protected void btnOk_Click(object sender, EventArgs e)
    {
        ClsAutoGeneNoDAL cls_AutoGeneNoDAL = new ClsAutoGeneNoDAL();
        string strQty = this.labQty.Value.ToString().Trim();

        //int labQty = int.Parse(strQty.Substring(0, strQty.IndexOf('.')));
        int labQty = int.Parse(strQty);
        int retuQty = cls_AutoGeneNoDAL.CheckIdentifyNos();
        if (retuQty == 0 || retuQty < labQty)
        {
            this.labMessage.Text = "<script language='javascript'>alert('产生的识别码数量与商品数量不一致！');</script>";
        }
        else
        {
            Boolean bSuccsess = cls_AutoGeneNoDAL.AddIntoPubIdentifys();
            if (bSuccsess)
            {
                this.labMessage.Text = "<script language='javascript'>alert('保存识别码成功！');window.close();</script>";
            }
            else
            {
                this.labMessage.Text = "<script language='javascript'>alert('保存识别码失败！');</script>";
            }
        }
    }    

    //根据开始序号自动求结束序号
    protected void txtBeginNo_TextChanged(object sender, EventArgs e)
    {
        //this.labMessage.Text = "<script language='javascript'>alert('" + this.txtBeginNo.Text.ToString().Trim() + iQty + "');</script>";
        //this.labMessage.Text = "<script language='javascript'>alert('" + iQty + "');</script>";
        string strQty = iQty.ToString();
        //string[] arriQty = strQty.Split(new char[]{'.'});
        int iEndNo = int.Parse(this.txtBeginNo.Text.ToString()) + int.Parse(strQty) - 1;//
        this.txtEndNo.Text = iEndNo.ToString().Trim();
    }

    //自动产生radio控件
    protected void dgrdIdentifyResult_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            Label chkRsSelect = (Label)e.Item.FindControl("chkRsSelect");
            //chkRsSelect.Text = "<input type=radio name='UserID' value=" + e.Item.Cells[0].Text.Trim() + ">";
            chkRsSelect.Text = "<input type=radio name='UserID'>";
        }
    }

    //修改当前的识别码
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        ClsAutoGeneNoDAL cls_AutoGeneNoDAL = new ClsAutoGeneNoDAL();
        string strCurIdentifyNo = this.hidCurIdenNo.Value.ToString().Trim();
        string strUpdIdentifyNo = this.txtUpdateIdentifyNo.Text.ToString().Trim();
        //this.labMessage.Text = "<script language='javascript'>alert('" + strCurIdentifyNo + strUpdIdentifyNo + "');</script>";
        Boolean bSuccess = cls_AutoGeneNoDAL.UpdateCurrentIdentify(strCurIdentifyNo, strUpdIdentifyNo);
        if (bSuccess)
        {
            getIdentifyNo(iFlag);
            this.txtUpdateIdentifyNo.Text = "";
        }
    }

    //清空识别码
    protected void btnClear_Click(object sender, EventArgs e)
    {
        ClsAutoGeneNoDAL cls_AutoGeneNoDAL = new ClsAutoGeneNoDAL();
        Boolean bSuccess = cls_AutoGeneNoDAL.DeleteTempIdentifys();
        if (bSuccess)
        {
            //this.getIdentifyNo(iFlag);
            this.dgrdIdentifyResult.DataSource = null;
            this.dgrdIdentifyResult.DataBind();
            this.labAlreadyQty.Text = "";
        }        
    }

    //作废当前选中的识别码
    protected void btnDel_Click(object sender, EventArgs e)
    {
        ClsAutoGeneNoDAL cls_AutoGeneNoDAL = new ClsAutoGeneNoDAL();
        string strCurIdentifyNo = this.hidCurIdenNo.Value.ToString().Trim();
        Boolean bSuccess = cls_AutoGeneNoDAL.DeleteCurrentIdentify(strCurIdentifyNo);
        if (bSuccess)
        {
            getIdentifyNo(iFlag);
        }        
    }

    //单条录入
    protected void btnContinue_Click(object sender, EventArgs e)
    {
        ClsAutoGeneNoDAL cls_AutoGeneNoDAL = new ClsAutoGeneNoDAL();        
        string strSinIdeNo = this.txtSingleNo.Text.ToString().Trim();//单条输入的识别码
        //隐藏值：单号，日期，商品编号，批号     
        string[] arrPara = getParams();
        Boolean bSuccess = cls_AutoGeneNoDAL.AddSingleIdentify(strSinIdeNo, arrPara[0], DateTime.Parse(arrPara[1]), arrPara[2], arrPara[3]);
        if (bSuccess)
        {
            getIdentifyNo(1);//此处代表查询单条产生的识别码
            this.txtSingleNo.Text = "";//清空
            ChangPage();
        }
    }

    //取消时清空临时产生的识别码表
    protected void btnCanel_Click(object sender, EventArgs e)
    {
        //ClsAutoGeneNoDAL cls_AutoGeneNoDAL = new ClsAutoGeneNoDAL();
        //Boolean bSuccess = cls_AutoGeneNoDAL.DeleteTempIdentifys();
        //ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        //cls_Property.SetOrGetQueryDataSet = null;
    }      

    public void ChangPage(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        DataSet dst = cls_Property.SetOrGetQueryDataSet;

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            string strKey = ((LinkButton)sender).CommandName;
            int intPageIndex = this.dgrdIdentifyResult.CurrentPageIndex;
            int intPageCounts = this.dgrdIdentifyResult.PageCount;

            //判断翻页方向            
            switch (strKey)
            {
                case "0":
                    intPageIndex = 0;
                    break;
                case "1":
                    if (intPageIndex > 0) intPageIndex -= 1;
                    break;
                case "2":
                    if (intPageIndex < intPageCounts - 1) intPageIndex += 1;
                    break;
                default:
                    intPageIndex = intPageCounts - 1;
                    break;            
            }

            this.BindQueryDataSet(dst, intPageIndex);
        }
    }

    public void ChangPage()
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        DataSet dst = cls_Property.SetOrGetQueryDataSet;

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            string strKey = "0";
            int intPageIndex = this.dgrdIdentifyResult.CurrentPageIndex;
            int intPageCounts = this.dgrdIdentifyResult.PageCount;

            //判断翻页方向
            if (strKey == "0")
            {
                intPageIndex = 0;
            }

            if (intPageCounts > 0)
            {
                this.lbtnQueryFirst.Enabled = this.lbtnQueryPrev.Enabled = this.lbtnQueryNext.Enabled = this.lbtnQueryEnd.Enabled = true;
            }

            this.BindQueryDataSet(dst, intPageIndex);
        }
    }

    private void BindQueryDataSet(DataSet dst, int CurrPageIndex)
    {
        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            this.dgrdIdentifyResult.CurrentPageIndex = CurrPageIndex;
            this.dgrdIdentifyResult.DataSource = dst;
            this.dgrdIdentifyResult.DataBind();

            int intPageCount = this.dgrdIdentifyResult.PageCount;
            int intCurrPage = this.dgrdIdentifyResult.CurrentPageIndex + 1;
            this.lblQueryRsCounts.Text = "共" + dst.Tables[0].Rows.Count + "条记录";
            this.lblQueryPageCounts.Text = "共" + intPageCount + "页";
            this.lblQueryCurrPage.Text = "当前第" + intCurrPage + "页";
            this.lblQueryRsCounts.Visible = this.lblQueryPageCounts.Visible = this.lblQueryCurrPage.Visible = true;
            this.lbtnQueryFirst.Visible = this.lbtnQueryPrev.Visible = this.lbtnQueryNext.Visible = this.lbtnQueryEnd.Visible = true;
            this.lbtnQueryFirst.Enabled = this.lbtnQueryPrev.Enabled = this.lbtnQueryNext.Enabled = this.lbtnQueryEnd.Enabled = true;
            this.lblQueryRsCounts.ForeColor = this.lblQueryPageCounts.ForeColor = this.lblQueryCurrPage.ForeColor = Color.FromArgb(78, 78, 78);

            if (intPageCount == 1)
            {
                this.lbtnQueryFirst.Enabled = this.lbtnQueryPrev.Enabled = this.lbtnQueryNext.Enabled = this.lbtnQueryEnd.Enabled = false;
            }
            else if (intCurrPage == intPageCount)
            {
                this.lbtnQueryNext.Enabled = this.lbtnQueryEnd.Enabled = false;
            }
            else if (intCurrPage - 1 == 0)
            {
                this.lbtnQueryFirst.Enabled = this.lbtnQueryPrev.Enabled = false;
            }
        }
    }

    protected void rdoSingle_CheckedChanged(object sender, EventArgs e)
    {
        this.txtSingleNo.Enabled = true;
        this.txtPrefix.Enabled = this.txtSuffix.Enabled = this.txtBeginNo.Enabled = this.txtEndNo.Enabled = false;
        this.txtPrefix.Text = this.txtSuffix.Text = this.txtBeginNo.Text = this.txtEndNo.Text = "";
        this.btnGeneration.Enabled = false;
        this.btnContinue.Enabled = true;
    }

    protected void rdoBatch_CheckedChanged(object sender, EventArgs e)
    {
        this.txtSingleNo.Enabled = false;
        this.txtSingleNo.Text = "";
        this.txtPrefix.Enabled = this.txtSuffix.Enabled = this.txtBeginNo.Enabled = this.txtEndNo.Enabled = true;
        this.btnGeneration.Enabled = true;
        this.btnContinue.Enabled = false;
    }

    //公共取传过来的参数(单号，日期，商品编号，批号 )
    private string[] getParams()
    {
        string getPara = this.labhidPara.Text.ToString().Trim();
        string[] arrPara = getPara.Split(new Char[] { '*' });
        return arrPara;
    }  
}
